﻿IF OBJECT_ID('up_KML_AssetXmissionStructures') IS not NULL
	DROP PROC up_KML_AssetXmissionStructures 

GO 


--[up_UtilityAssetXMissionStructuresPoles_SelectForKML] 4

CREATE PROCEDURE [dbo].[up_KML_AssetXmissionStructures]
	 @id int 
AS

DECLARE @TranmissionStructuresList varchar(MAX)
SET @TranmissionStructuresList = ''

SELECT 
	 @TranmissionStructuresList = @TranmissionStructuresList + CONVERT(varchar, [Utility_Asset_XMissionStructures_PoleId]) + ','
FROM 
	v_tb_Utility_Asset_XMissionStructures_Poles Pole
INNER JOIN tb_Utility_Asset_XMissionStructures Structure
	ON Structure.Utility_Asset_XMissionStructureId = Pole.Utility_Asset_XMissionStructureFK
WHERE 
	Structure.UtilityFK = @id

declare @settingValue varchar(500)
set @settingValue = (select SettingValue from tb_Settings where SettingId = 26)

declare @template varchar(max);
set @template = '
	<iframe 
		src="{url}{page}?ID=$[ID]" 
		style="width: 325px; height: 400px;"
		frameborder="0"  marginwidth="0" marginheight="0">Pole Position</iframe>
		$[description]'
select @template = replace(@template, '{url}', SettingValue)
from tb_Settings where SettingId = 23

declare @xml varchar(max);
select @xml = replace(convert(varchar(max),
(select 

	 'Pole Position Desktop' as 'Document/name'

	-- style info
	,(select
		'AssetXmission' as '@id'
		,.75 as 'IconStyle/scale'
		-- brown
		,'ff0055aa' as 'IconStyle/color'
		,replace(@template, '{page}', 'AssetXmission.aspx') as 'BalloonStyle/text'
		,Replace(@settingValue, '{0}', 'mapicon_asset_transmission.png') as 'IconStyle/Icon/href'
		for xml path('Style'), type) as 'Document'
	
	-- point: Transmission Structure (Asset)
	,(select 

		-- what test shold show up on the map?
		List.Value as 'name'

		-- all assets of this type get the same base style
		,'#AssetXmission' as 'styleUrl'

		-- should we show a special icon?
		--,@TranmissionStructuresIcon as 'Style/IconStyle/Icon/href'

		-- should we show it in a special color?
		--,@TranmissionStructuresColor as 'Style/IconStyle/color'

		,'ID' as 'ExtendedData/Data/@name'
		,List.Value as 'ExtendedData/Data/value'

		-- where is it?
		,Convert(varchar, Longitude, 128) + ',' + Convert(varchar, Latitude, 128) + ',0' as 'Point/coordinates'
	from
		(select * from dbo.fn_Split(@TranmissionStructuresList, default)) as List
		join tb_Utility_Asset_XMissionStructures as Structures 
			on Structures.Utility_Asset_XMissionStructureId = Convert(int, List.Value)
	for xml path('Placemark'), TYPE) as 'Document'
	
for xml path('kml'))), '<kml>', '<?xml version="1.0" encoding="UTF-8"?><kml xmlns="http://earth.google.com/kml/2.2">')

select @xml;











 